package com.tjsinfo.backworkflow.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.tjsinfo.backworkflow.service.IBackWorkFlowService;
import com.tjsinfo.backworkflow.service.ICommendService;
import com.tjsinfo.backworkflow.vo.EdocOpinion;
import com.tjsinfo.backworkflow.vo.EdocSummary;


@Controller
@RequestMapping("/backworkflow")
public class WorkFlowController {
	
	@Value("#{config[DATE]}")
	private  String DATE;
	@Value("#{config[IP]}")
	private  String IP;
	@Value("#{config[domainName]}")
	private  String domainName;
	
	@Autowired
	private IBackWorkFlowService backWorkFlowService;
	
	@Autowired
	private ICommendService commendService;
	
	@RequestMapping("/mainview")
	public String main(){
		return "main";
	}
	
	@RequestMapping("/selectFile")
	@ResponseBody
	public  Object selectFile(String title){		
		List<EdocSummary> list = new ArrayList<EdocSummary>();
		if(commendService.compare_date(DATE)){
			if(!"".equals(title) && title != null){
				list = backWorkFlowService.selectTitle(title);
			}			
		}
		return list;
		
	}
	
	@RequestMapping("/selectopinionview")
	public ModelAndView selectopinionview(Long id){
		ModelAndView modelAndView = new ModelAndView("selectopinionview");
		modelAndView.addObject("id", id);
		return modelAndView;
	}
	
	@RequestMapping("/selectopinion")
	@ResponseBody
	public  Object selectopinion(Long id){		
		List<EdocOpinion> list = new ArrayList<EdocOpinion>();
		if(commendService.compare_date(DATE)){
			list = backWorkFlowService.selectOpinion(id);
		}
		return list;
	}
	
	@RequestMapping("/selectOneOpinion")
	@ResponseBody
	public  Object selectOneOpinion(Long id){
		EdocOpinion edocOpinion = new EdocOpinion();
		if(commendService.compare_date(DATE)){
			edocOpinion = backWorkFlowService.selectOneOpinion(id);	
		}
		return edocOpinion;
	}
	
	@RequestMapping("/updateopinion")
	@ResponseBody
	public  Object updateopinion(Long contentId,String content){		
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(commendService.compare_date(DATE)){
			if(backWorkFlowService.updateOpinion(contentId.toString(), content)){
				map.put("state", true);
			}else{
				map.put("state", false);
			}
		}
		
		return map;
	}
	
	@RequestMapping("/deleteopinion")
	@ResponseBody
	public  Object deleteopinion(Long contentId){		
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(commendService.compare_date(DATE)){
			if(backWorkFlowService.deleteOpinion(contentId.toString())){
				map.put("state", true);
			}else{
				map.put("state", false);
			}
		}
		
		return map;
	}
	
	@RequestMapping("/loginmail")
	public ModelAndView loginmail(String username){
		String url = commendService.mailLogin(username, DATE,IP,domainName);
		ModelAndView modelAndView = new ModelAndView(new RedirectView(url));		
		return modelAndView;
	}
	
	
}
